/**
 * Created by wang on 2015-10-15.
 * 栏目图片
 */
define(['angular'], function (angular) {
    "use strict";

    var directive = function (InfoService) {
        return {
            restrict: "E",
            replace : true,
            template: '<div class="kBanner"><img  ng-src="{{channel.large_icon}}" alt="{{channel.catalog_name}}" /></div>',
            link: function(scope, element, attrs) {
                var channelId;
                if(attrs["channelId"]) {
                    channelId = attrs["channelId"];
                } else {
                    channelId = scope.channel.channelId;
                }

                if(channelId) {
                    getChannelInfos(channelId, scope);
                } else {
                    scope.$watch("channel.channelId", function(newValue, oldValue){
                        if(newValue) {
                            getChannelInfos(newValue, scope);
                        }
                    });
                }
            }
        }

        function getChannelInfos(channelId, scope){
            InfoService.getChannelInfo(channelId).then(function(data){
                scope.channel.large_icon = data.large_icon;
                scope.channel.catalog_name = data.catalog_name;

            });
        }
    };

    directive.$inject = ['InfoService'];
    return directive;
});